package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 登录或者注册方法
     * @param phone  用户输入的手机号
     * @param inputCode  用户 输入验证码
     * @param verifyCode  系统的验证码
     * @return
     */
    @Override
    public User login(String phone, String inputCode, String verifyCode) {
        //1. 校验验证码
        User user = null;
        if(inputCode.equalsIgnoreCase(verifyCode)) {
            //2. 如果验证码没有问题，查询该用户的手机号是否存在
            user = userMapper.findByPhone(phone);
            if(user==null){
                //3. 如果不存在帮该用户创建一个新用户，并且插入数据库中
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userMapper.save(user);
            }
        }
        return user;
    }
}
